package com.hanrui.android.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hanrui.android.R;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.model.User;
import com.hanrui.android.net.contract.login.LoginContract;
import com.hanrui.android.net.presenter.login.LoginPresenter;
import com.hanrui.android.utils.BaseUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * 登录
 * Created by zhengwei on 2018/3/9.
 */
public class LoginActivity extends ActionBarBaseActivity<LoginPresenter> implements LoginContract.View {
    @BindView(R.id.phone)
    EditText mEtUsername;
    @BindView(R.id.yzm)
    EditText mEtPassword;
    @BindView(R.id.yzm_iv)
    ImageView eyeIv;
    @BindView(R.id.yzm_tv)
    RelativeLayout eyeRl;
    @BindView(R.id.input_ll)
    LinearLayout inputLl;
    @BindView(R.id.login)
    TextView login;
    int eye = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        unbinder = ButterKnife.bind(this);

        mTitleBar.setLeftVisible(false);

        initView();
    }

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }


    protected void initView() {

        mTitleBar.setImmersive(false);
        mTitleBar.setVisibility(View.GONE);
        mImmersionBar.statusBarDarkFont(true, 0.2f).init();

        mEtUsername.setText(SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.USERNAME_KEY));
        mEtPassword.setText(SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.PASSWORD_KEY));
    }


    @Override
    public void loginSuccess(User user) {
        SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.USERNAME_KEY, mEtUsername.getText().toString());
        SPUtils.getInstance(Constant.SHARED_NAME).put(Constant.PASSWORD_KEY, mEtPassword.getText().toString());
        BaseUtils.saveUser(this, user);
        JPushInterface.setAlias(this,1,user.getUserId());
        openActivityAndCloseThis(MainActivity.class);
    }


    @OnClick({R.id.yzm_tv, R.id.login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.yzm_tv:
                if (eye == 0){
                    eyeIv.setImageResource(R.mipmap.icon_eay_open22x);
                    mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    eye = 1;
                }else{
                    eyeIv.setImageResource(R.mipmap.eyeicon22x);
                    eye = 0;
                    mEtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.login:
                String username = mEtUsername.getText().toString();
                String password = mEtPassword.getText().toString();
                if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
                    ToastUtils.showShort(R.string.the_username_or_password_can_not_be_empty);
                    return;
                }
                mPresenter.login(username, password);
                break;
        }
    }


    public static void start(Context context){
        Intent intent = new Intent(context,LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }
}
